package Day06;
import java.util.Scanner;
import java.util.Arrays;
public class LevelGuessLetters {

	public static void main(String[] args) {
		//猜的次数
		int count = 0;
		Scanner scan = new Scanner(System.in);
		//用户输入等级
		int level;
		while(true) {
			p("请输入游戏级别:(5,7,9)？");
			level = scan.nextInt();
			if(level == 5 || level == 7 || level == 9){
				break;
			}
		}
		//生成标准答案
		char generate[] = generate(level);
		p(Arrays.toString(generate));
		p("请输入你所猜的"+ level + "个字母序列:(exit---退出)");
		while(true) {
			String content = scan.next().trim().toUpperCase();
			if("EXIT".equals(content)) {
				p("已退出");
				break;
			}
			char input[] = content.toCharArray();
			int result[] = compare(generate,input);
			count++;
			if(result[0] == 5) {
				p("恭喜猜对了,得分" + (500 - count * 10));
				break;
			}else {
				int sum = result[0] + result[1];
				p("你猜对了"+sum+"个字符，其中"+ result[0] + "个字符的位置正确!(总次数="+ count + ",exit---退出");
			}
		}
		
	}
	
	//生成随机字母数组
	public static char[] generate(int level) {
		char letters[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
				'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		boolean flags[] = new boolean[letters.length];
		char generate[] = new char[level];
		for(int i = 0; i < level; i++ ) {
			int index;
			//去重复
			do {
				index = (int)(Math.random() * letters.length);
			}while(flags[index]);
			flags[index] = true;
			generate[i] = letters[index];
		}
		return generate;
	}

	public static void p(String str) {
		System.out.println(str);
	}
	
	//对比
	public static int[] compare(char[] generate,char[] input) {
		int num1 = 0;
		int num2 = 0;
		for(int i = 0;i < input.length; i++) {
			for(int j = 0; j < generate.length; j++) {
				if(input[i] == generate[j]) {
					if(i == j) {
						num1 += 1;
						break;
					}
					num2 += 1;
				}
			}
		}
		System.out.println(num1 + "," + num2);
		int compare[] = {num1,num2};
		return compare;
	}
}
